import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'cart_page.dart';
import 'category_page.dart';
import 'home_page.dart';
import 'member_page.dart';
import 'package:provide/provide.dart';
import '../provide/currentIndex.dart';

class IndexPage extends StatelessWidget {
  final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(
      icon:Icon(CupertinoIcons.home),
      title:Text('首页')
    ),
    BottomNavigationBarItem(
      icon:Icon(CupertinoIcons.search),
      title:Text('分类')
    ),
    BottomNavigationBarItem(
      icon:Icon(CupertinoIcons.shopping_cart),
      title:Text('购物车')
    ),
     BottomNavigationBarItem(
      icon:Icon(CupertinoIcons.profile_circled),
      title:Text('会员中心')
    ),
  ];

   final List<Widget> tabBodies = [
      HomePage(),
      CategoryPage(),
      CartPage(),
      MemberPage()
   ];

  @override
  Widget build(BuildContext context) {
   
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
  
    return Provide<CurrentIndexProvide>(

      builder: (context,child,val){
        //------------关键代码----------start---------
        int currentIndex= Provide.value<CurrentIndexProvide>(context).currentIndex;
        // ----------关键代码-----------end ----------
        return Scaffold(
            backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
            bottomNavigationBar: BottomNavigationBar(
              type:BottomNavigationBarType.fixed,
              currentIndex: currentIndex,
              items:bottomTabs,
              onTap: (index){
                //------------关键代码----------start---------
                Provide.value<CurrentIndexProvide>(context).changeIndex(index);
                // ----------关键代码-----------end ----------
              },
            ),
             body: IndexedStack(
                    index: currentIndex,
                    children: tabBodies
                  ),
        ); 
      }
    );
     
  }
}
// class IndexPage extends StatefulWidget {
//   @override
//   _IndexPageState createState() => _IndexPageState();
// }

// class _IndexPageState extends State<IndexPage> {
//   //定义了底部导航的文字和使用的图标
//   final List<BottomNavigationBarItem> bottomTabs = [
//     BottomNavigationBarItem(icon: Icon(CupertinoIcons.home), title: Text('首页')),
//     BottomNavigationBarItem(
//         icon: Icon(CupertinoIcons.search), title: Text('分类')),
//     BottomNavigationBarItem(
//         icon: Icon(CupertinoIcons.shopping_cart), title: Text('购物车')),
//     BottomNavigationBarItem(
//         icon: Icon(CupertinoIcons.profile_circled), title: Text('会员中心')),
//   ];
//   //切换页面
//   final List<Widget> tabBodies = [
//     HomePage(),
//     CategoryPage(),
//     CartPage(),
//     MemberPage()
//   ];

//   int currentIndex = 0; //负责tabBodies的List索引，改变索引就相当于改变了页面
//   var currentPage; //利用currentIndex得到当前选择的页面，并进行呈现出来

//   @override
//   void initState() {
//     currentPage = tabBodies[currentIndex];
//     super.initState();
//   }

//   @override
//   Widget build(BuildContext context) {
//     ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)
//       ..init(context); //适配
// //    print('设备宽度:${ScreenUtil.screenWidth}');
// //    print('设备高度:${ScreenUtil.screenHeight}');
// //    print('设备像素密度:${ScreenUtil.pixelRatio}');
//     return Scaffold(
//       backgroundColor: Colors.redAccent,
//       bottomNavigationBar: BottomNavigationBar(
//         type: BottomNavigationBarType.fixed, //设置底部tab的样式
//         currentIndex: currentIndex,
//         items: bottomTabs,
//         onTap: (index) {
//           setState(() {
//             //页面转换间的状态重置
//             currentIndex = index;
//             currentPage = tabBodies[currentIndex];
//           });
//         },
//       ),
//       body: IndexedStack(
//         index: currentIndex,
//         children: tabBodies, //接收数组，返回的是listWidget
//       ),
//     );
//   }
// }
